home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 11 / develop 11 code / The NetWork Project / Examples (Sources) / NetSim / Task.p < prev    next >
Encoding:
Text File  |  1992-07-15  |  7.1 KB  |  286 lines  |  [TEXT/MPS ]

  1. {$S Task}
  2. UNIT Task;
  3. {© G. Sawitzki, StatLab Heidelberg 1986-1991}    
  4.  
  5. INTERFACE
  6.  
  7. USES
  8. MacUnits,
  9. StdTools,Generic,NetWork,NetWorkLookUp,ObjIntf,SchedulerUnit,NetsimGlobal,
  10. UToy,
  11. HistogramUnit,HistogramIO;
  12.  
  13.  
  14.  
  15. CONST
  16.     MyFileType = 'TEXT'; {Your default file type for files you create}
  17.     cDefaultDistribution='GAUS';
  18.     cDefaultDisplay='SCAT';
  19.     mDistribution=4; {additional menus}
  20.     mDisplay=5;
  21.     mControl=128;
  22.  
  23. VAR
  24.     {channel file identification through the standard tools package}
  25.     OutSFReply, InSFReply : TFileInfo;  {use these variables, for example}
  26.     OutFile, InFile : Text;
  27.  
  28.  
  29.     gNextNetRefresh:longint;
  30.  
  31.     {----------------- Main Routines    ------------------}
  32.  
  33.     {You will have to change these routines to adapt to your needs. 
  34.     Arrange your program in a reentrant way. 
  35.     You can define (global) state variables in this unit.}
  36.  
  37.  
  38. PROCEDURE MyInit (FileType : OSType);
  39.     {Will be called once when the application starts. Initialize
  40.     your global variables in MyInit. FileType generically is set
  41.     to the constant MyFileType.
  42.     In MacApp the corresponding method is TEvtHandler.IApplication.}
  43.  
  44. PROCEDURE MyTask (phase : BackPhase);
  45.     {Application equivalent of system Task: this is the core of
  46.     your main program. It will be called periodically unless
  47.     more important events are pending. phase will show you whether
  48.     something happened in the meantime -- see the TYPE BackPhase
  49.     defined in StdTools.
  50.     In MacApp the corresponding method is TEvtHandler.DoIdle.}
  51.  
  52. PROCEDURE MyCleanUp;
  53.     {will be called once when the application is about to finish.
  54.     Save your results here, clean up any gaarbage you have made.}
  55.  
  56.  
  57.     {-------------------- routines on request    -------------------------}
  58.     {These routines are called when you are asked to acces a document, eg.
  59.     by a menu selection. The CmdNumbers are constants defined in StdTools}
  60.  
  61.  
  62. PROCEDURE doopen (itsCmdNumber : cmdnumber);
  63.  
  64. PROCEDURE dosave (itsCmdNumber : cmdnumber);
  65.  
  66. PROCEDURE doclose (itsCmdNumber : cmdnumber);
  67.  
  68.  
  69.     IMPLEMENTATION
  70. var gProtocolOn:boolean;
  71.     gnrResults:longint;    {last shown to user}
  72.     nrResults:longint;    {delta since last shown to user}
  73.  
  74. procedure DebugStrNum(s:str255;n:longint);
  75. var s1:str255;
  76. begin
  77.     NumToString(n,s1);
  78.     debugstr(concat(s,' ',s1));
  79. end;
  80.  
  81. type    tConfigurationHandle=^tConfigurationPtr;
  82.     tConfigurationPtr=^tConfiguration;
  83.     tConfiguration=record
  84.         version:longint;
  85.  
  86.         Protocolversion:integer;{not used}
  87.         {MPW Pascal has boolean bytes; Resedit words= byte+pad!}
  88.         MasterProtocolOn:boolean;
  89.         pad2:boolean;
  90.         SlaveProtocolOn:boolean;
  91.         pad21:boolean;
  92.  
  93.         ShowControlMenu:boolean;
  94.         pad22:boolean;
  95.  
  96.  
  97.         GenerateSamples:boolean; {not used. should switch to a totally controlled situation}
  98.         pad3:boolean;
  99.         UseDelays:boolean;        {add a delay to task. }
  100.         pad4:boolean;
  101.  
  102.         RandomDelays:boolean;    {use a random delay. uses exponential distribution}
  103.         pad5:boolean;
  104.         UseFailureRates:boolean;    {use a random delay. uses exponential distribution}
  105.         pad6:boolean;
  106.         UseMessageSizes:boolean;    {use a random delay. uses exponential distribution}
  107.         pad7:boolean;
  108.  
  109.         TaskCoreTime:integer;    {used for master as well}
  110.         TaskDefinitionTime:integer;    {used for master as well}
  111.         TaskUpdateTime:integer;    {used for master as well}
  112.         ReplyFailureRate:integer;    {used for master as well}
  113.         AcceptFailureRate:integer;    {used for master as well}
  114.         SampleSize:integer;        
  115.  
  116.         MinPrioSize:integer;    {not used}
  117.         MinCoreSize:integer;    {not used}
  118.  
  119.         UseBroadCast:boolean;    {use -1 broacast address}
  120.         pad8:boolean;
  121.  
  122.         MaxNrNodes:    integer;
  123.         ComputationGrainSize: integer;
  124.         TickleInterval:    integer;
  125.         WaitInterval:    integer;
  126.     end;
  127. var theConfiguration:tConfiguration;
  128.  
  129.     {*************************************************}
  130.  
  131.  
  132. var gMsgHeader:tMsgHeader; {holds distribution token, displaytoken, samplesize, version}
  133.  
  134.     function LongintStr(l:longint):str15;
  135. var trick:packed record 
  136.         case boolean of
  137.         true:(s:signedbyte;sl:longint);
  138.         false:(s15:str15);
  139.     end;
  140. begin
  141.     trick.s:=4;
  142.     trick.sl:=l;
  143.     longintstr:=trick.s15;
  144. end;
  145.  
  146. function MsgHeaderStr(var hdr:tMsgHeader):str255;
  147.  
  148. begin
  149.     with hdr do MsgHeaderStr:=concat(Longintstr(ContentsToken),' ',
  150.     Longintstr(DistributionToken),' ',
  151.     Longintstr(DisplayToken));
  152. end;
  153.  
  154. procedure prepareMessageHeader(var aMsgHeader:tMsgHeader;myAction:tAction);
  155. begin
  156.     {set correct info to header}
  157.     with aMsgHeader do begin
  158.         sampleSize:=TheConfiguration.SampleSize;
  159.         version:=cMyMsgHeaderVersion;
  160.         traceInfo:=gnrResults+nrResults;
  161.         action:=myAction;
  162.  
  163.         DistributionToken:=longint(cDefaultDistribution);
  164.  
  165.         DisplayToken:=longint(cDefaultDisplay);
  166.     end;
  167. end;
  168.  
  169. {*************************************************}
  170. var    localhist,totalhist:histtype;
  171.     localstat,totalstat:tStatType;
  172.     localhrect,totalhrect:rect;
  173.     localsrect,totalsrect:rect;
  174.     pcomprect,qcomprect:rect;
  175.     nform:decform;
  176.  
  177. var StatisticWindow:WindowPtr;
  178.     StatisticWindowRec:WindowRecord;
  179.     {implementation for handlers}
  180.  
  181. procedure initStatistics;
  182. var safeport:grafptr;myPic:PicHandle;
  183. begin
  184.     initHistogram(localhist,'local');
  185.     initHistogram(totalhist,'total');
  186.     initstat(localstat,'local');
  187.     initstat(totalstat,'total');
  188.     if statisticwindow<>nil then begin
  189.         getport(safeport);
  190.         setport(statisticwindow);
  191.         mypic:=getwindowPic(statisticwindow);
  192.         if mypic<>nil then killpicture(myPic);
  193.         setwindowpic(statisticwindow,nil);
  194.         eraserect(thePort^.portrect);
  195.         setport(safeport);
  196.     end;
  197. end;
  198.  
  199. procedure DrawStatisticWindow;
  200. var safeport:grafptr;myPic:PicHandle;
  201. begin
  202.     if statisticwindow<>nil then begin
  203.         getport(safeport);
  204.         setport(statisticwindow);
  205.         mypic:=getwindowPic(statisticwindow);
  206.         if mypic<>nil then killpicture(myPic);
  207.         mypic:=OpenPicture(thePort^.portrect);
  208.         showhistogram(localhist,localhrect,nform);
  209.         showhistogram(totalhist,totalhrect,nform);
  210.         reportstat(localstat,localsrect,nform);
  211.         reportstat(totalstat,totalsrect,nform);
  212.         closepicture;
  213.         setwindowpic(statisticwindow,mypic);
  214.         eraserect(thePort^.portrect);
  215.         drawpicture(mypic,thePort^.portrect);
  216.         validrect(thePort^.portrect);
  217.         setport(safeport);
  218.     end;
  219. end;
  220.  
  221. var BaseWindow:WindowPtr;
  222.     baserect:rect;
  223.     RecordWindow:WindowPtr;
  224.     minRect,maxRect:rect;
  225.     minPic,maxPic:PicHandle;
  226.     minrecord,maxrecord:extended;
  227. { discard garbage below }
  228. var 
  229.     gSoldResults:str255;
  230.     gsoldResults2:Str255;
  231.     goldTime:longint;
  232.  
  233.     sLastTicks:longint;
  234.     sMeanTicks:real;
  235.  
  236. const     
  237.     TicksDelta=300;
  238.     cNetRefreshDelta=60;
  239. var     nextTicks:longint;
  240.     oldTime:longint;
  241.     sResults,sTicks:str255;
  242.     
  243. { $I statistics.impl.p}
  244.  
  245. procedure sInit;
  246. begin
  247.     sLastTicks:=tickCount;
  248.     sMeanTicks:=100;    {a random number to start off-  we are using provisional means}
  249. end;
  250.  
  251. procedure NoteStatistics(x:extended;islocal:boolean);
  252. begin
  253.     if IsLocal then begin 
  254.     AddStat(x,localstat);addhist(x,localhist);
  255.     end;
  256.     
  257.     AddStat(x,totalstat);addhist(x,totalhist);
  258. end;
  259.  
  260. procedure NewRecordPicture;
  261. var myPic:PicHandle;var savePort:GrafPtr;
  262. begin
  263.     GetPort(SavePort);
  264.     SetPort(RecordWindow);
  265.     myPIc:=GetWindowPic(RecordWindow);
  266.     if myPic<>nil then killPicture(myPic);
  267.     myPic:=openPicture(RecordWindow^.portrect);
  268.     showpen;
  269.     EraseRect(RecordWindow^.portrect);
  270.     {framerect(minrect);fillrect(minrect,ltGray);}
  271.     DrawPicture(minPic,MinRect);
  272.     DrawPicture(maxPic,MaxRect);
  273.     
  274.     hidepen;
  275.     closePicture;
  276.     setwindowpic(RecordWindow,myPic);
  277.     SetPort(SavePort);
  278. end;
  279.  
  280. {$I taskCore.impl.p}
  281. {$I task.handlers.impl.p}
  282. {$I task.master.impl.p}
  283. {$I task.reply.impl.p}
  284. {$I task.slave.impl.p}
  285. {$I task.impl.p}
  286. END.